mui.init();
(function($) {
    //阻尼系数
    var deceleration = mui.os.ios ? 0.003 : 0.0009;
    $('.mui-scroll-wrapper').scroll({
        bounce: false,
        indicators: true, //是否显示滚动条
        deceleration: deceleration
    });
    // 初始化数据
    var msprovince, mscity, mssex, msage_range, page = 1;
    var messpage = 1;
    // 创建数据模型
    msUrl = encodeURI('http://localhost/ypshow/mobile/client_api.php?act=get_nearby_user');
    // var msUrl = encodeURI('http://www.xiaiyepu.cn/mobile/client_api.php?act=get_nearby_user');

    messUrl = encodeURI('http://localhost/ypshow/mobile/client_api.php?act=get_history_msg_test');
    // var messUrl = encodeURI('http://www.xiaiyepu.cn/mobile/client_api.php?act=get_history_msg');

    delUrl = encodeURI('http://localhost/ypshow/mobile/client_api.php?act=delete_history_test');
    // delUrl = encodeURI('http://www.xiaiyepu.cn/mobile/client_api.php?act=delete_history');

    var msPostData = {};
    var messPostData = {};

    function getPostData() {
        if (!!msprovince) {
            msPostData.province = msprovince;
        } else {
            delete msPostData.province;
        }
        if (!!mscity) {
            msPostData.city = mscity;
        } else {
            delete msPostData.city;
        }
        if (!!mssex) {
            msPostData.sex = mssex;
        } else {
            delete msPostData.sex;
        }
        if (!!msage_range) {
            msPostData.age_range = msage_range;
        } else {
            delete msPostData.age_range;
        }

        if (!!page) {
            msPostData.page = page;
        } else {
            delete msPostData.page;
        }

        return msPostData;
    }

    function getMessData() {
        if (!!messpage) {
            messPostData.page = messpage;
        } else {
            delete messPostData.page;
        }
        return messPostData;
    }

    // 数据处理
    var msnear = new Vue({
        el: '#ms-near',
        data: {
            items: []
        },
        methods: {
            calDis: function(distance) {
                if (distance < 1000)
                    return (distance + "米");
                else if (distance > 1000)
                    return ((Math.round(distance / 100) / 10).toFixed(1) + "公里")
            },
            calTim: function(ltime) {
                var ltime = ltime * 1000;
                var timestamp = Date.parse(new Date());
                var diff = timestamp - ltime;
                var content = null;
                if (diff < 5 * 60 * 1000 /*5分钟内*/ ) {
                    content = "刚刚";
                } else if (diff < 60 * 60 * 1000 /*60分钟内*/ ) {
                    content = Math.floor(diff / 60 / 1000) + "分钟前";
                } else if (diff < 24 * 60 * 60 * 1000 /*24小时内*/ ) {
                    content = Math.floor(diff / 60 / 60 / 1000) + "小时前";
                } else {
                    content = Math.floor(diff / 24 / 60 / 60 / 1000) + "天前";
                }
                return content;
            },
            go: function(id) {
                var userUrl = "http://www.baidu.com?id=" + id; //跳转链接
                mui.openWindow({
                    url: userUrl
                });
            }
        }
    })

    // 消息的数据模型
    var msmessages = new Vue({
        el: '#ms-messages',
        data: {
            items: []
        },
        methods: {
            chTime: function(ltimestamp) {
                var timestamp = Date.parse(new Date());
                var ltimestamp = parseInt(ltimestamp) * 1000;
                console.log(ltimestamp);
                console.log(timestamp)
                var time = new Date(ltimestamp);
                var lD = new Date(timestamp);
                var diff = timestamp - ltimestamp;

                if (lD.getDate() != time.getDate()) {
                    if (diff > 24 * 60 * 60 * 1000 /*24小时内*/ ) {

                    }
                }

                var y = time.getFullYear();
                var m = time.getMonth() + 1;
                var d = time.getDate();
                var h = time.getHours();
                var mm = time.getMinutes();
                var s = time.getSeconds();
                return '' + m + '-' + d + ' ' + h + ':' + mm;
            },
            go: function(id) {
                var userUrl = "http://www.baidu.com?id=" + id; //跳转链接
                mui.openWindow({
                    url: userUrl
                });
            }
        }
    })

    // 初始化数据内容
    function getInitData() {
        var postParam = getPostData();
        var messParam = getMessData();
        mui.post(msUrl, postParam, function(data) {
            if (data.c & !!data.o) {
                var newContent = [];
                for (var i in data.o) {
                    var content = {
                        user_id: data.o[i].user_id,
                        user_name: data.o[i].user_name,
                        headimg: data.o[i].headimg,
                        sex: data.o[i].sex,
                        age: data.o[i].age,
                        location_time: data.o[i].location_time,
                        distance: data.o[i].distance
                    }
                    msnear.items.push(content);
                }
                page++;
            }
        });
        mui.post(messUrl, messParam, function(data) {
            if (data.c & !!data.o) {
                var newContent = [];
                for (var i in data.o) {
                    var content = {
                        from_id: data.o[i].from_id,
                        to_id: data.o[i].to_id,
                        record_content: data.o[i].record_content,
                        record_time: data.o[i].record_time,
                        msg_cont: data.o[i].msg_cont,
                        user_id: data.o[i].to_user.user_id,
                        user_name: data.o[i].to_user.user_name,
                        headimg: data.o[i].to_user.headimg
                    }
                    msmessages.items.push(content);
                    // console.log(content)
                }

                messpage++;
            }
        });
    }
    getInitData();


    function loaddata() {
        var postParam = getPostData();
        mui.post(msUrl, postParam, function(data) {
            if (data.c & !!data.o) {
                var newContent = [];
                for (var i in data.o) {
                    var content = {
                        user_id: data.o[i].user_id,
                        user_name: data.o[i].user_name,
                        headimg: data.o[i].headimg,
                        sex: data.o[i].sex,
                        age: data.o[i].age,
                        location_time: data.o[i].location_time,
                        distance: data.o[i].distance
                    }
                    msnear.items.push(content);
                }
                page++;
                if (data.e.totalPage <= data.e.currentPage) {

                    mui('#scroll1').pullRefresh().endPullupToRefresh(true);
                } else {

                    mui('#scroll1').pullRefresh().endPullupToRefresh(false);
                }
            } else {
                mui('#scroll1').pullRefresh().endPullupToRefresh(true);
            }
        });
    }



    $.ready(function() {
        //循环初始化所有下拉刷新，上拉加载。
        $.each(document.querySelectorAll('.mui-slider-group .mui-scroll'), function(index, pullRefreshEl) {
            $(pullRefreshEl).pullToRefresh({
                up: {
                    callback: function() {
                        if (index == 1) {
                            var self = this;
                            var messParam = getMessData();
                            mui.post(messUrl, messParam, function(data) {
                                if (data.c & !!data.o) {
                                    var newContent = [];
                                    for (var i in data.o) {
                                        var content = {
                                            from_id: data.o[i].from_id,
                                            to_id: data.o[i].to_id,
                                            record_content: data.o[i].record_content,
                                            record_time: data.o[i].record_time,
                                            msg_cont: data.o[i].msg_cont,
                                            user_id: data.o[i].to_user.user_id,
                                            user_name: data.o[i].to_user.user_name,
                                            headimg: data.o[i].to_user.headimg
                                        }
                                        msmessages.items.push(content);
                                    }
                                    if (data.e.totalPage <= data.e.currentPage) {
                                        self.endPullUpToRefresh(true);
                                    } else {
                                        self.endPullUpToRefresh(false);
                                    }
                                    messpage++;
                                } else {
                                    self.endPullUpToRefresh(true);
                                }
                            });
                        } else {
                            var self = this;
                            var postParam = getPostData();
                            mui.post(msUrl, postParam, function(data) {
                                if (data.c & !!data.o) {
                                    var newContent = [];
                                    for (var i in data.o) {
                                        var content = {
                                            user_id: data.o[i].user_id,
                                            user_name: data.o[i].user_name,
                                            headimg: data.o[i].headimg,
                                            sex: data.o[i].sex,
                                            age: data.o[i].age,
                                            location_time: data.o[i].location_time,
                                            distance: data.o[i].distance
                                        }
                                        msnear.items.push(content);
                                    }
                                    page++;
                                    if (data.e.totalPage <= data.e.currentPage) {
                                        self.endPullUpToRefresh(true);
                                    } else {
                                        self.endPullUpToRefresh(false);
                                    }
                                    // console.log(page);
                                } else {
                                    self.endPullUpToRefresh(true);
                                }
                            });
                        }
                    }
                }
            });
        });
    });
    // 导航栏附近的显示
    document.querySelector('.mui-slider').addEventListener('slide', function(event) {
        //注意slideNumber是从0开始的；
        if (event.detail.slideNumber) {
            document.querySelector('.ms-near-circle').setAttribute('style', 'visibility:hidden;');
            document.querySelector('.ms-near-screen').setAttribute('style', 'display:none;');
        } else {
            document.querySelector('.ms-near-circle').setAttribute('style', 'visibility: visible;');
            document.querySelector('.ms-near-screen').setAttribute('style', 'display:inline;');
        }
    });

    //删除效果
    var btnArray = ['确认', '取消'];
    $('#yty-tabk').on('tap', '.mui-btn', function(event) {
        var elem = this;
        var li = elem.parentNode.parentNode;
        mui.confirm('确认删除该条记录？', '您确定不约？', btnArray, function(e) {
            if (e.index == 0) {
                var from_id = li.getAttribute('ms-data');
                console.log(from_id + 'from_id')
                if (!!from_id) {
                    mui.post(delUrl, {
                        from_id: from_id
                    }, function(data) {
                        if (!!data.c) {
                            mui.toast('删除成功', {
                                duration: 'short',
                                type: 'div'
                            })
                            li.parentNode.removeChild(li);
                        } else {
                            mui.toast('删除失败', {
                                duration: 'short',
                                type: 'div'
                            })
                        }
                    });
                } else {
                    mui.toast('删除失败', {
                        duration: 'short',
                        type: 'div'
                    })
                }

            } else {
                setTimeout(function() {
                    $.swipeoutClose(li);
                }, 0);
            }
        });
    });
})(mui);

//筛选函数
function doscreen() {
    window.location.href = "http://www.baidu.com";
}